package com.ojoin.ojoin.eb_spider.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.ojoin.ojoin.eb_spider.service.SupplyPaymentService;
import com.ojoin.trade.common.utils.CommonUtils;
import com.ojoin.trade.common.utils.LogUtils;
import com.ojoin.trade.common.utils.TimeUtils;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlSupplyPayment;
@Service
public class SupplyPaymentServiceImpl implements SupplyPaymentService {
	
	private Logger log = LoggerFactory
			.getLogger(SupplyPaymentServiceImpl.class);
	
	
	@Override
	public void removeSettlementCommon(List<TbSlSupplyPayment> list,
			Map<String, TbSlSupplyPayment> MapDB) {

		if (!CommonUtils.isEmpty(MapDB)) {
			Set<String> set = MapDB.keySet();

			if (!CommonUtils.isEmpty(list)) {
				LogUtils.info(log, "结算单据:" + list.size());
				Iterator<TbSlSupplyPayment> it = list.iterator();
				while (it.hasNext()) {
					TbSlSupplyPayment payment = it.next();
					if (set.contains(payment.getSettlementId())) {

						it.remove();

						MapDB.remove(payment.getSettlementId());

						LogUtils.info(log, "结算单据:" + payment.getSettlementId()
								+ "已存在");

					}else{
						LogUtils.info(log, "结算单据:" + payment.getSettlementId()
								+ "不存在");
					}
				}
			}
		} else {
			if (!CommonUtils.isEmpty(list)) {
				LogUtils.info(log, "结算单据:" + list.size());
				Iterator<TbSlSupplyPayment> it = list.iterator();
				while (it.hasNext()) {
					TbSlSupplyPayment payment = it.next();
					LogUtils.info(log, "结算单据:" + payment.getSettlementId()
							+ "不存在");
				}
			}
		}

	}

	@Override
	public void removePaymentCommon(List<TbSlSupplyPayment> list,
			Map<String, TbSlSupplyPayment> MapDB) {
		if (!CommonUtils.isEmpty(MapDB)) {
			Set<String> set = MapDB.keySet();

			if (!CommonUtils.isEmpty(list)) {
				LogUtils.info(log, "付款单据:" + list.size());

				Iterator<TbSlSupplyPayment> it = list.iterator();
				while (it.hasNext()) {
					TbSlSupplyPayment payment = it.next();
					if (set.contains(payment.getPayId())) {

						it.remove();

						MapDB.remove(payment.getPayId());

						LogUtils.info(log, "付款单据:" + payment.getPayId() + "已存在");

					}else{
						LogUtils.info(log, "付款单据:" + payment.getSettlementId()
								+ "不存在");
					}
				}
			}
		} else {
			if (!CommonUtils.isEmpty(list)) {
				LogUtils.info(log, "付款单据:" + list.size());

				Iterator<TbSlSupplyPayment> it = list.iterator();
				while (it.hasNext()) {
					TbSlSupplyPayment payment = it.next();

					LogUtils.info(log, "付款单据:" + payment.getPayId() + "不存在");

				}
			}
		}

	}

	@Override
	public List<TbSlSupplyPayment> changeToPay(
			Map<String, TbSlSupplyPayment> MapDB) {
		if (!CommonUtils.isEmpty(MapDB)) {
			Collection<TbSlSupplyPayment> co = MapDB.values();
			Iterator<TbSlSupplyPayment> it = co.iterator();
			while (it.hasNext()) {
				TbSlSupplyPayment tbsl = it.next();

				tbsl.setSettlementState(TbSlSupplyPayment.COMPLETEPAY);
				tbsl.setPayDate(TimeUtils.getCurDate());

				LogUtils.info(log, "结算单据:" + tbsl.getSettlementId()
						+ "不存在与结算单，已付款，状态更新");
			}
			
			return new ArrayList<TbSlSupplyPayment>(MapDB.values());
		}

		
		return null;
	}

	
	@Override
	public List<TbSlSupplyPayment> changeToPay(
			Map<String, TbSlSupplyPayment> MapDB,List<TbSlSupplyPayment> pay) {
		
	   List<TbSlSupplyPayment> re = new ArrayList<TbSlSupplyPayment>();
		
		if (!CommonUtils.isEmpty(MapDB) && CommonUtils.isEmpty(pay)) {
			
			Iterator<TbSlSupplyPayment> it = pay.iterator();
			while (it.hasNext()) {
				TbSlSupplyPayment tbsl = it.next();

				if(!CommonUtils.isEmpty(MapDB.get(tbsl.getSettlementId()))){
					tbsl.setSettlementState(TbSlSupplyPayment.COMPLETEPAY);
					tbsl.setPayDate(TimeUtils.getCurDate());

					re.add(tbsl);
					LogUtils.info(log, "结算单据:" + tbsl.getSettlementId()
							+ "不存在与结算单，已付款，状态更新");
				}
				
			}
			
			return re;
		}

		
		return re;
	}
	
	
	
}
